<template>
  <view v-if="visible" class="achievement-popup">
    <view class="achievement-container" :class="{ 'show': visible }">
      <view class="achievement-header">
        <image class="achievement-star" src="/static/icons/dice.png" mode="aspectFit" />
        <text class="achievement-title">成就解锁！</text>
      </view>
      <view class="achievement-content">
        <image class="achievement-icon" :src="achievement.icon" mode="aspectFit" />
        <view class="achievement-info">
          <text class="achievement-name">{{ achievement.title }}</text>
          <text class="achievement-desc">{{ achievement.description }}</text>
        </view>
      </view>
      <view class="achievement-reward">
        <text class="reward-text">+{{ achievement.energyReward }} 能量</text>
      </view>
      <view class="achievement-close" @click="close">
        <text>知道了</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'AchievementPopup',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    achievement: {
      type: Object,
      default: () => ({
        title: '成就解锁',
        description: '恭喜你解锁了一个新成就！',
        icon: '/static/icons/dice.png',
        energyReward: 1
      })
    }
  },
  data() {
    return {
      // 音效
      achievementSound: null
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.playSound()
      }
    }
  },
  mounted() {
    this.initSound()
  },
  methods: {
    initSound() {
      try {
        this.achievementSound = uni.createInnerAudioContext()
        this.achievementSound.src = '/static/sounds/achievement.mp3'
        this.achievementSound.autoplay = false
      } catch (error) {
        console.error('初始化成就音效失败:', error)
      }
    },
    playSound() {
      if (this.achievementSound) {
        this.achievementSound.stop()
        this.achievementSound.play()
      }
    },
    close() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss">
.achievement-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
  
  .achievement-container {
    width: 80%;
    background-color: #fff;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    transform: scale(0.8);
    opacity: 0;
    transition: all 0.3s ease;
    
    &.show {
      transform: scale(1);
      opacity: 1;
    }
    
    .achievement-header {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 15px;
      
      .achievement-star {
        width: 24px;
        height: 24px;
        margin-right: 8px;
      }
      
      .achievement-title {
        font-size: 18px;
        font-weight: bold;
        color: #ff9800;
      }
    }
    
    .achievement-content {
      display: flex;
      align-items: center;
      padding: 10px 0;
      
      .achievement-icon {
        width: 60px;
        height: 60px;
        margin-right: 15px;
      }
      
      .achievement-info {
        flex: 1;
        
        .achievement-name {
          font-size: 16px;
          font-weight: bold;
          color: #333;
          margin-bottom: 5px;
          display: block;
        }
        
        .achievement-desc {
          font-size: 14px;
          color: #666;
          display: block;
        }
      }
    }
    
    .achievement-reward {
      background-color: #f5f5f5;
      border-radius: 20px;
      padding: 8px 15px;
      display: flex;
      justify-content: center;
      margin: 15px 0;
      
      .reward-text {
        font-size: 14px;
        color: #4caf50;
        font-weight: bold;
      }
    }
    
    .achievement-close {
      margin-top: 10px;
      padding: 10px;
      background-color: #f0f0f0;
      border-radius: 20px;
      text-align: center;
      
      text {
        font-size: 14px;
        color: #666;
      }
    }
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.achievement-icon {
  animation: pulse 1s infinite;
}
</style>